package com.powergisol.gis.controller;

import com.powergisol.gis.entity.TabService;
import com.powergisol.gis.service.RemoteService;
import com.powergisol.gis.service.TabCoordinateService;
import com.powergisol.gis.service.TabDataService;
import com.powergisol.gis.util.Result;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Date: 2019/7/26  14:10
 * Description:
 */
@RestController
@RequestMapping("remote")
public class RemoteController {
    private static Logger logger = LoggerFactory.getLogger(RemoteController.class);
    @Autowired
    private RemoteService remoteService;
    @Autowired
    private TabDataService tabDataService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TabCoordinateService tabCoordinateService;

    @Value("${expira_time}")
    private Integer expiraTime;

    @RequestMapping(value = "upVisitTimes",method = RequestMethod.GET)
    public void upVisitTimes(String name,String uuid){
        if(!redisTemplate.hasKey(uuid)){
            TabService tabService = tabDataService.getByName(name);
            if(null == tabService){
                return;
            }
            tabService.setVisitTimes(tabService.getVisitTimes()+1);
            tabDataService.updateTabService(tabService);
            redisTemplate.opsForValue().set(uuid,uuid,4,TimeUnit.HOURS);
            logger.info("访问次数增加一次：{}", uuid);
        }
    }

    @GetMapping("getTabServiceByName")
    @ApiOperation(value = "获取服务详情", notes = "获取服务详情")
    public Result getTabServiceByName(String name){
        logger.info("3dmodel 获取服务详情：{}", name);
        Result result = tabDataService.getTabServiceByNameForRemote(name);
        return result;
    }

    /**
     * 获取服务文件地址和样式地址
     * @param name 服务名称
     * @return
     */
    @GetMapping("/getServiceFilePath")
    public ResponseEntity getServiceFilePath(String name,String uuid) {
        return remoteService.getServiceFilePath(name,uuid);
    }


    /**
     * 首页统计服务类型
     * @param p  登录用户
     * @return
     */
    @GetMapping("/countServicceType")
    public Map<String,Object> countServicceType(String userId,String userName,Principal p) {
        if(StringUtils.isEmpty(userName)){
            userName = p.getName();
        }
        return remoteService.countServicceType(userId,userName);
    }

    /**
     * 根据编号获取坐标系的proj4js
     * @param epsg  坐标系编码
     * @return
     */
    @GetMapping("/selectProByEspg")
    public Map<String, Object> selectProByEspg(String epsg) {
        return tabCoordinateService.selectProByEspg(epsg);
    }

}
